import { ListRowState } from "../../types"

interface CarState {
	carList: CarRowState[]
}

export interface CarRowState extends ListRowState {
	count: number
	isChecked: boolean
}

const ADDCAR = "ADDCAR"
const SUBCAR = "SUBCAR"
const DELETECAR = "DELETECAR"
const SWITCHCAR = "SWITCHCAR"
const TOTALSIWTCHCAR = "TOTALSIWTCHCAR"
const SUBMITORDER = 'SUBMITORDER'


interface TotalSwitchCarAction {
	type: typeof TOTALSIWTCHCAR
	payload: boolean
}

export function totalSwitchCarActionCreator(payload: boolean): TotalSwitchCarAction {
	return { type: TOTALSIWTCHCAR, payload }
}


interface SwitchCarAction {
	type: typeof SWITCHCAR
	payload: CarRowState
}

export function switchCarActionCreator(payload: CarRowState): SwitchCarAction {
	return { type: SWITCHCAR, payload }
}


interface AddCarAction {
	type: typeof ADDCAR
	payload: ListRowState
}

export function addCarActionCreator(payload: ListRowState): AddCarAction {
	return { type: ADDCAR, payload }
}

interface SubCarAction {
	type: typeof SUBCAR
	payload: ListRowState
}

export function subCarActionCreator(payload: ListRowState): SubCarAction {
	return { type: SUBCAR, payload }
}

interface DeleteCarAction {
	type: typeof DELETECAR
	payload: ListRowState
}

export function deleteCarActionCreator(payload: ListRowState): DeleteCarAction {
	return { type: DELETECAR, payload }
}

interface SubmitOrder {
	type: typeof SUBMITORDER
}

export function submitOrderActionCreator():SubmitOrder  {
	return { type: SUBMITORDER }
}


type CarAction = AddCarAction | SubCarAction | DeleteCarAction | SwitchCarAction | TotalSwitchCarAction | SubmitOrder


const initState: CarState = {
	carList: []
}

const carReducer = (state = initState, action: CarAction): CarState => {
	switch (action.type) {
		case ADDCAR: {
			// action.payload 列表项数据
			// 1. 同类数据是否存在
			const item = state.carList.find(v => v.id === action.payload.id);

			if (item) {
				// 存在
				item.count++
			} else {
				// 不存在
				state.carList.push({ ...action.payload, count: 1, isChecked: false })
			}

			return {
				...state,
				carList: [...state.carList]
			}
		}
		case SUBCAR: {
			// action.payload 列表项数据
			// 1. 同类数据是否存在
			const item = state.carList.find(v => v.id === action.payload.id);

			if (item) {
				// 存在
				item.count--
			}

			return {
				...state,
				carList: [...state.carList]
			}
		}
		case DELETECAR: {
			return {
				...state,
				carList: state.carList.filter(v => v.id !== action.payload.id)
			}
		}
		case SWITCHCAR: {
			// action.payload 列表项数据
			// 1. 同类数据是否存在
			const item = state.carList.find(v => v.id === action.payload.id);

			if (item) {
				// 存在
				item.isChecked = !item.isChecked
			}

			return {
				...state,
				carList: [...state.carList]
			}
		}
		case TOTALSIWTCHCAR: {
			state.carList.forEach(v => v.isChecked = action.payload);
			return {
				...state,
				carList: [...state.carList]
			}
		}
		case SUBMITORDER: {
			return {
				...state,
				carList: state.carList.filter(v => !v.isChecked)
			}
		}
	}
	return state
}

export default carReducer